package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dao.CmsArchivesCodeDao;
import com.ruoyi.system.domain.dto.CmsArchivesTagDto;
import com.ruoyi.system.domain.po.CmsArchivesCodeTag;
import com.ruoyi.system.domain.po.CmsArchivesCodeTagExample;
import com.ruoyi.system.domain.po.CmsArchivesTag;
import com.ruoyi.system.domain.po.CmsArchivesTagExample;
import com.ruoyi.system.domain.vo.CmsArchivesTagVo;
import com.ruoyi.system.mapper.CmsArchivesCodeTagMapper;
import com.ruoyi.system.mapper.CmsArchivesTagMapper;
import com.ruoyi.system.service.CmsArchivesTagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CmsArchivesTagServiceImpl implements CmsArchivesTagService {

    @Resource
    private CmsArchivesCodeTagMapper cmsArchivesCodeTagMapper;
    @Resource
    private CmsArchivesTagMapper cmsArchivesTagMapper;
    @Resource
    private CmsArchivesCodeDao dao;

    @Override
    public PageInfo<CmsArchivesTagVo.Result> query(Byte type, String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CmsArchivesTagVo.Result> results = dao.queryIds(type, name);
        PageInfo<CmsArchivesTagVo.Result> pageInfo = new PageInfo<>(results);
        if (CollectionUtil.isNotEmpty(results)) {
            List<Long> collect = pageInfo.getList().stream().map(CmsArchivesTagVo.Result::getId).collect(Collectors.toList());
            List<CmsArchivesTagVo.Result> query = dao.query(collect);
            pageInfo.setList(query);
        }

        return pageInfo;
    }

    @Override
    public Integer add(CmsArchivesTagDto.Add add) {
        CmsArchivesTag cmsArchivesTag = BeanUtil.copyProperties(add, CmsArchivesTag.class);
        if (add.getId() == null) {
            cmsArchivesTag.setId(IdUtil.getSnowflake().nextId());
            cmsArchivesTag.setCreateBy(SecurityUtils.getUsername());
            cmsArchivesTag.setCreateTime(new Date());
            cmsArchivesTagMapper.insertSelective(cmsArchivesTag);

            if (CollectionUtil.isNotEmpty(add.getCodeList())) {
                for (String code : add.getCodeList()) {
                    CmsArchivesCodeTag codeTag = new CmsArchivesCodeTag();
                    codeTag.setArchivesTagId(cmsArchivesTag.getId());
                    codeTag.setCode(code);
                    codeTag.setId(IdUtil.getSnowflake().nextId());
                    codeTag.setCreateBy(SecurityUtils.getUsername());
                    codeTag.setCreateTime(new Date());
                    cmsArchivesCodeTagMapper.insertSelective(codeTag);
                }
            }
        }else {
            cmsArchivesTag.setUpdateBy(SecurityUtils.getUsername());
            cmsArchivesTag.setUpdateTime(new Date());
            cmsArchivesTagMapper.updateByPrimaryKeySelective(cmsArchivesTag);
            if (CollectionUtil.isNotEmpty(add.getCodeList())) {
                //删除关联再添加
                CmsArchivesCodeTagExample cmsArchivesCodeTagExample = new CmsArchivesCodeTagExample();
                cmsArchivesCodeTagExample.createCriteria().andDelFlagEqualTo((byte) 0).andArchivesTagIdEqualTo(cmsArchivesTag.getId());
                CmsArchivesCodeTag cmsArchivesCodeTag = new CmsArchivesCodeTag();
                cmsArchivesCodeTag.setDelFlag((byte) 1);
                cmsArchivesCodeTagMapper.updateByExampleSelective(cmsArchivesCodeTag, cmsArchivesCodeTagExample);

                for (String code : add.getCodeList()) {
                    CmsArchivesCodeTag codeTag = new CmsArchivesCodeTag();
                    codeTag.setArchivesTagId(cmsArchivesTag.getId());
                    codeTag.setCode(code);
                    codeTag.setId(IdUtil.getSnowflake().nextId());
                    codeTag.setCreateBy(SecurityUtils.getUsername());
                    codeTag.setCreateTime(new Date());
                    cmsArchivesCodeTagMapper.insertSelective(codeTag);
                }
            }
        }
        return 1;
    }

    @Override
    public Integer del(List<Long> idList) {
        //删除关联再添加
        CmsArchivesTagExample cmsArchivesCodeTagExample = new CmsArchivesTagExample();
        cmsArchivesCodeTagExample.createCriteria().andDelFlagEqualTo((byte) 0).andIdIn(idList);
        CmsArchivesTag cmsArchivesCodeTag = new CmsArchivesTag();
        cmsArchivesCodeTag.setDelFlag((byte) 1);
        return cmsArchivesTagMapper.updateByExampleSelective(cmsArchivesCodeTag, cmsArchivesCodeTagExample);
    }
}
